package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/admin/setmeal")
@RestController("adminSetmealController")
@Slf4j
@Api(tags = "套餐相关接口")
public class SetmealController {

    @Autowired
    SetmealService setmealService;

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result<PageResult> pageSelect(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("分页查询-page：{}，pageSize{}", setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        return Result.success(setmealService.pageSelect(setmealPageQueryDTO));
    }

    @ApiOperation("新增套餐")
    @PostMapping
    public Result<String> addSetmeal(@RequestBody SetmealDTO setmealDTO){
        setmealService.addSetmeal(setmealDTO);
        return Result.success();
    }

    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    @ApiOperation("修改套餐启用禁用")
    @PostMapping("/status/{status}")
    public Result<String> switchStartOrStop(@PathVariable Integer status, Long id){
        setmealService.switchStartOrStop(status, id);
        return Result.success();
    }

    @ApiOperation("批量删除套餐")
    @DeleteMapping
    public Result<String> deleteByIds(@RequestParam List<Long> ids){
        setmealService.deleteByIds(ids);
        return Result.success();
    }

    @ApiOperation("根据id查询套餐")
    @GetMapping("/{id}")
    public Result<SetmealVO> selectById(@PathVariable Long id){
        return Result.success(setmealService.selectById(id));
    }

    @CacheEvict(cacheNames = {"setmealCache", "setmealDishCache"}, allEntries = true)
    @ApiOperation("修改套餐")
    @PutMapping
    public Result<String> updateSetmeal(@RequestBody SetmealVO setmealVO){
        setmealService.updateSetmeal(setmealVO);
        return Result.success();
    }
}

